<?php
class Gallery
{
	private $_yaht;
	private $_pict;
	
	public function __construct()
	{
		$this->_yaht = new Zend_Db_Table('gallery_albums');
		$this->_pict = new Zend_Db_Table('gallery_pictures');
	}
	
	public function getList($Quantity = 0)
	{
		$SelectYaht = $this->_yaht->select()->order('Name');
		
		if ($Quantity>0) $SelectYaht = $SelectYaht->limit($Quantity);

		$Rows = $this->_yaht->fetchAll($SelectYaht);
		
		if ($Rows)
		{
		  
			$Yahts = $Rows->toArray();
		  foreach ($Yahts as $Key => $Item)
		  {
		  	$Select = $this->_pict->select()->where('yaht_id = ?',$Item['id']);
            $Pictures = $this->_pict->fetchAll($Select);
            if ($Pictures)
            {
                $Pictures = $Pictures->toArray();
            }
            else
            {
                $Pictures = array();
            }
            $Yahts[$Key]['pictures'] = $Pictures;
		  } 
			return  $Yahts;
		}
		return array();
	}
	
	
   public function getListA($Quantity = 0)
    {
        $SelectYaht = $this->_yaht->select()->order('Name')->where("Purpose = 'on'");
        
        if ($Quantity>0) $SelectYaht = $SelectYaht->limit($Quantity);

        $Rows = $this->_yaht->fetchAll($SelectYaht);
        
        if ($Rows)
        {
          
            $Yahts = $Rows->toArray();
          foreach ($Yahts as $Key => $Item)
          {
            $Select = $this->_pict->select()->where('yaht_id = ?',$Item['id']);
            $Pictures = $this->_pict->fetchAll($Select);
            if ($Pictures)
            {
                $Pictures = $Pictures->toArray();
            }
            else
            {
                $Pictures = array();
            }
            $Yahts[$Key]['pictures'] = $Pictures;
          } 
            return  $Yahts;
        }
        return array();
    }
	
    public function getOne($YID)
    {
        $Yaht = $this->_yaht->find($YID);
        if ($Yaht)
        {
        	$arrYaht = $Yaht->current();
        	if (!$arrYaht) return false;
        	$arrYaht  = $arrYaht->toArray();
        	$Select = $this->_pict->select()->where('yaht_id = ?',$YID);
        	$Pictures = $this->_pict->fetchAll($Select);
        	if ($Pictures)
        	{
        		$Pictures = $Pictures->toArray();
        	}
        	else
        	{
        		$Pictures = array();
        	}
        	
        	$arrYaht['pictures'] = $Pictures;
        	return $arrYaht;
        }
        else
        {
        	return false;
        	//TODO:   throw Exception
        }
    }
    
    
    
    
    public function getOneByName($Name)
    {
        $Yaht = $this->_yaht->fetchRow(  $this->_yaht->select()->where('name = ?', $Name)   );

        if ($Yaht)
        {
        	$arrYaht = $Yaht;
        	$YID = $Yaht->id;
        	if (!$arrYaht) return false;
        	$arrYaht  = $arrYaht->toArray();
        	$Select = $this->_pict->select()->where('yaht_id = ?',$YID);
        	$Pictures = $this->_pict->fetchAll($Select);
        	if ($Pictures)
        	{
        		$Pictures = $Pictures->toArray();
        	}
        	else
        	{
        		$Pictures = array();
        	}
        	
        	$arrYaht['pictures'] = $Pictures;
        	return $arrYaht;
        }
        else
        {
        	return false;
        }
    }
    
    
   
    
    public function save($YID,$Params)
    {
        if (!isset($Params['Purpose']))   $Params['Purpose'] = "";
    	$Yaht = $this->_yaht->update($Params, 'id = ' . intval($YID));
    }
    
    public function savePicture($PID,$Params)
    {
    	$Yaht = $this->_pict->update($Params, 'id = ' . intval($PID));
    }
    
    public function delete($YID)
    {
        $Yaht = $this->_yaht->delete('id = ' . intval($YID));
    }
    
    
    public function create()
    {
        $data = array(
                    'Name'        => 'Новый объект',
                    'Description' => ''
        );
    	$Yaht = $this->_yaht->createRow($data);
    	$Yaht->save();
    	return $Yaht->id; 
    }
    
    public function insertPictures($YID, $Pictures)
    {
    	foreach ($Pictures as $File)
        {
        	$this->_pict->createRow(array('filename'=>$File,'yaht_id'=>$YID))->save();
        }
    }
    
    
    public function deletePicture($PID)
    {
        $this->_pict->delete('id = ' . intval($PID));
    }
    
    
	
	
}